package com.androidtv.pos.adapter;

import android.view.View;

import com.androidtv.pos.R;
import com.androidtv.pos.adapter.viewholder.RightMealViewHolder;
import com.androidtv.pos.modelbean.HomeArticleData;
import com.chad.library.adapter.base.BaseQuickAdapter;

import androidx.annotation.NonNull;

public class RightMealAdapter extends BaseQuickAdapter<HomeArticleData, RightMealViewHolder> {


    private onButtonClickListener mListener;

    public RightMealAdapter(int layoutResId) {
        super(layoutResId);

    }


    //   int   itemId;
    @Override
    protected void convert(@NonNull RightMealViewHolder rightMealViewHolder, HomeArticleData item) {

        rightMealViewHolder.setText(R.id.tv_project_title,item.getTitle());

        rightMealViewHolder.setText(R.id.tv_meal_plus, item.getCurrentNum()+"");

        rightMealViewHolder.setOnClickListener(R.id.tv_meal_plus, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mListener != null) {
                    int newNum = item.getCurrentNum() + 1;
                    item.setCurrentNum(newNum);
                    mListener.onAdapterAddClick(item);
                }
                notifyItemChanged(rightMealViewHolder.getAdapterPosition());
            }
        });

        rightMealViewHolder.setOnClickListener(R.id.image_meal_minus, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mListener != null) {
                    int newNum = item.getCurrentNum() - 1 < 1 ? 0 : item.getCurrentNum() - 1;
                    item.setCurrentNum(newNum);
                    mListener.onAdapterMinusClick(item);

                    notifyItemChanged(rightMealViewHolder.getAdapterPosition());
                }
            }
        });

        rightMealViewHolder.setOnClickListener(R.id.tv_project_title, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (item.getCurrentNum()>0){
                    if (mListener != null) {
                        mListener.onAdaptrPreviewClick(item);
                    }
                }
            }
        });



    }


//    public void isClickedPlus(boolean isClickedPlus, int num, int id, int position) {
//
//        this.isClickedPlus = isClickedPlus;
//        this.mMum = num;
//        this.id = id;
//        this.position = position;
//        notifyItemChanged(position);
//
//    }
//
//
//    private boolean isClickedMinus;
//    private int numMinus;
//    private int idMinus;
//    private int positionMinus;
//
//
//    public void isClickedMinus(boolean isClickedMinus, int numMinus, int idMinus, int positionMinus) {
//        this.isClickedMinus = isClickedMinus;
//        this.numMinus = numMinus;
//        this.idMinus = idMinus;
//        this.positionMinus = positionMinus;
//
//        notifyItemChanged(positionMinus);
//
//    }


    public interface onButtonClickListener {
        void onAdapterAddClick(HomeArticleData data);

        void onAdapterMinusClick(HomeArticleData data);

        void onAdaptrPreviewClick(HomeArticleData data);
    }


   public void addListener(onButtonClickListener listener) {
        mListener = listener;
    }
}
